import { GameConfig } from "../configs/GameConfig"
import { PlayerHeadUI, PlayerHeadUIPool } from "../gameEtc/PlayerHeadUI"

@Component
export default class Appearance extends mw.Script{

    static AppearanceMap:Map<number,Appearance> = new Map()

    @mw.Property({multicast:true, replicated:true, onChanged:'onOwnerPlayerIDChanged'})
    ownerPlayerID:number
    @mw.Property({multicast:true, replicated:true, onChanged:'onOwnerPlayerNameChanged'})
    playerName:string
    @mw.Property({multicast:true, replicated:true, onChanged:'onClothCfgIDChanged'})
    clothCfgID:number = 0
    @mw.Property({multicast:true, replicated:true, onChanged:'onHandEffectCfgIDChanged'})
    handEffectCfgID:number = 0

    ownerPlayer:Player
    headUI:PlayerHeadUI

    private playedHandEffectID:number[] = []

    init(ownerPlayerID:number){
        this.ownerPlayerID = ownerPlayerID
        Appearance.AppearanceMap.set(this.ownerPlayerID, this)
    }

    private onOwnerPlayerIDChanged(){
        Appearance.AppearanceMap.set(this.ownerPlayerID, this)
        this.ownerPlayer = Player.getPlayer(this.ownerPlayerID)
        this.onClothCfgIDChanged()
        this.onHandEffectCfgIDChanged()
    }

    private onOwnerPlayerNameChanged(){
        if(!this.ownerPlayer || !this.ownerPlayer.character) return
        if(!this.headUI){
            this.headUI = PlayerHeadUIPool.get()
        }
        this.headUI.setInfo(this.ownerPlayer.character, this.playerName)
    }

    private onClothCfgIDChanged(){
        if(!this.ownerPlayer || !this.ownerPlayer.character) return
        if(this.clothCfgID == 0) return

        let clothCfg = GameConfig.Cloth.getElement(this.clothCfgID)
        this.ownerPlayer.character.asyncReady().then((character) => {
            character.setDescription([clothCfg.Data[0]])
        })
    }

    private onHandEffectCfgIDChanged(){
        if(!this.ownerPlayer || !this.ownerPlayer.character) return
        if(this.handEffectCfgID == 0) return

        this.removeEffect()
        
        let clothCfg = GameConfig.Cloth.getElement(this.handEffectCfgID)
        this.ownerPlayer.character.asyncReady().then((character) => {
            let sf = Number(clothCfg.Data[1])
            let scale = new Vector(sf,sf,sf)
            let playID = EffectService.playOnGameObject(clothCfg.Data[0], character, {
                slotType:HumanoidSlotType.RightHand,
                loopCount:0,
                scale:scale
            })
            let playID2 = EffectService.playOnGameObject(clothCfg.Data[0], character, {
                slotType:HumanoidSlotType.LeftHand,
                loopCount:0,
                scale:scale
            })
            this.playedHandEffectID.push(playID, playID2)
        })
    }

    private removeEffect(){
        for (const playedID of this.playedHandEffectID) {
            EffectService.stop(playedID)
        }
        this.playedHandEffectID = []
    }

    protected onDestroy(): void {
        Appearance.AppearanceMap.delete(this.ownerPlayerID)
    }
}